package jehc.djshi.sys.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jehc.djshi.log.client.service.LogsUtil;
import jehc.djshi.sys.dao.XtDepartInfoDao;
import jehc.djshi.sys.model.XtDepartInfo;
import jehc.djshi.sys.service.XtDepartInfoService;
import jehc.djshi.sys.service.XtPostService;
import org.springframework.stereotype.Service;
import jehc.djshi.common.base.BaseService;
import jehc.djshi.common.util.ExceptionUtil;
import javax.annotation.Resource;
/**
 * @Desc 部门信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class XtDepartInfoServiceImpl extends BaseService implements XtDepartInfoService {

	@Resource
    XtDepartInfoDao xtDepartinfoDao;

	@Resource
	XtPostService xtPostService;

	@Resource
	LogsUtil logsUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<XtDepartInfo> getXtDepartInfoListByCondition(Map<String,Object> condition){
		try {
			return xtDepartinfoDao.getXtDepartInfoListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public XtDepartInfo getXtDepartInfoById(String id){
		try {
			return xtDepartinfoDao.getXtDepartInfoById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param xtDepartInfo
	* @return
	*/
	public int addXtDepartInfo(XtDepartInfo xtDepartInfo){
		int i = 0;
		try {
			xtDepartInfo.setCreateId(getXtUid());
			xtDepartInfo.setCreateTime(getDate());
			i = xtDepartinfoDao.addXtDepartInfo(xtDepartInfo);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("部门业务类", "添加", "执行添加部门成功");
		} catch (Exception e) {
			logsUtil.aBLogs("部门业务类", "添加", "执行修改部门失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param xtDepartInfo
	* @return
	*/
	public int updateXtDepartInfo(XtDepartInfo xtDepartInfo){
		int i = 0;
		try {
			xtDepartInfo.setUpdateId(getXtUid());
			xtDepartInfo.setUpdateTime(getDate());
			i = xtDepartinfoDao.updateXtDepartInfo(xtDepartInfo);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("部门业务类", "修改", "执行修改部门成功");
		} catch (Exception e) {
			logsUtil.aBLogs("部门业务类", "修改", "执行修改部门失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delXtDepartInfo(Map<String,Object> condition){
		int i = 0;
		try {
			String[] idList = (String[])condition.get("id");
			for(String id:idList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("parentId", id);
				//验证下级部门是否存在（正常部门）
				if(!xtDepartinfoDao.getXtDepartInfoListChild(map).isEmpty()){
					logsUtil.aBLogs("部门业务类", "删除", "执行删除部门，编号【"+id+"】存在下级部门，不能删除");
					return 0;
				}
				//验证是否存在岗位（正常岗位）
				map = new HashMap<String, Object>();
				map.put("id", id);
				if(!xtPostService.getXtPostInfoList(map).isEmpty()){
					logsUtil.aBLogs("部门业务类", "删除", "执行删除部门，编号【"+id+"】存在被岗位使用中，不能删除");
					return 0;
				}
			}
			i = xtDepartinfoDao.delXtDepartInfo(condition);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("部门业务类", "删除", "执行删除部门成功");
		} catch (Exception e) {
			logsUtil.aBLogs("部门业务类", "删除", "执行删除部门失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
	
	/**
	 * 部门根目录集合
	 * @return
	 */
	public List<XtDepartInfo> getXtDepartInfoList(){
		try {
			return xtDepartinfoDao.getXtDepartInfoList();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 查找子集合
	 * @param condition
	 * @return
	 */
	public List<XtDepartInfo> getXtDepartInfoListChild(Map<String,Object> condition){
		try {
			return xtDepartinfoDao.getXtDepartInfoListChild(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 查找所有集合
	 * @param condition
	 * @return
	 */
	public List<XtDepartInfo> getXtDepartInfoListAll(Map<String,Object> condition){
		try {
			return xtDepartinfoDao.getXtDepartInfoListAll(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param condition
	 * @return
	 */
	public List<XtDepartInfo> queryXtDepartInfoList(Map<String,Object> condition){
		return xtDepartinfoDao.queryXtDepartInfoList(condition);
	}
}
